package com.jiepos.mpos.common.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.jiepos.mpos.common.entity.App_Log_AbstractEntity;
import com.jiepos.mpos.core.dao.BaseDao;


/**
 * 
 * 
 * <p>标题: —— 提现申请表操作类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.common.dao 
 * @author 张学涛
 * @version 1.0
 * @since 2017年10月16日上午11:40:40
 * @history（历次修订内容、修订人、修订时间等）
 */
@Repository
public class LogAbstractDao extends BaseDao<App_Log_AbstractEntity> {
	
	
	public List<Map<String, String>> getUnknownWithdraw() {
		List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
		String selectSql = "select t.ins_uuid, t.apply_no,t.custom_uuid,t.custom_bankcard_uuid from T_APP_LOG_ABSTRACT t "
				+ "where t.audit_stlflag = '0' and t.rsp_code in ('XX','000') for update";				
		log.debug("selectSql sql=[{}]", selectSql);
		String updateSql = "update T_APP_LOG_ABSTRACT t set t.audit_stlflag = '2' where t.apply_no = ?";
		log.debug("updateSql sql=[{}]", updateSql);
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(selectSql);			
			rs = ps.executeQuery();
			while (rs.next()) {
				String apply_no = rs.getString(2);				
				ps = conn.prepareStatement(updateSql);
				ps.setString(1, apply_no);
				ps.executeUpdate();
				Map<String, String> dataMap = new HashMap<String, String>();
				dataMap.put("apply_no", apply_no);				
				dataList.add(dataMap);
			}
			conn.commit();
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
			try {
				if(conn != null){
				conn.rollback();
				}
			} catch (SQLException e1) {
				log.error(e1.getMessage(), e1);
			}
		} finally {			
			free(conn, ps, rs);
		}
		return dataList;
	}
	
	
	/** 
     * 建立数据库连接 
     *  
     * @return 
     * @throws SQLException 
     */  
    public Connection getConnection() throws SQLException {  
        Connection conn = null;  
        conn = jdbcTemplate.getDataSource().getConnection();;  
        return conn;  
    }
    
    
    /**
	 * 释放资源
	 * 
	 * @param conn
	 * @param statement
	 * @param rs
	 */
	private void free(Connection conn, PreparedStatement statement,ResultSet set) {
		if (set != null) {
			freeResultSet(set);
		}
		if (statement != null) {
			freeStatement(statement);
		}
		if (conn != null) {
			freeConnection(conn);
		}		
	}
	
	/**
	 * 释放连接
	 * 
	 * @param conn
	 */
	private static void freeConnection(Connection conn) {
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 释放PreparedStatement
	 * 
	 * @param statement
	 */
	private static void freeStatement(PreparedStatement statement) {
		try {
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 释放PreparedStatement
	 * 
	 * @param statement
	 */
	private static void freeResultSet(ResultSet set) {
		try {
			set.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
